package com.umeox.watch.moto.launcher.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by zl on 2017/10/17.
 */

public class CurvedScrollBar extends View {

    private static final String TAG = "CurvedScrollBar";
    private Context context;
    private Paint paint;//圆弧画笔
    private Paint valuePaint;//移动圆弧画笔
    private int centerX;//中心X
    private int centerY;//中心Y
    private int srcH;//控件高度
    private float startAngle = -20;//圆弧起始角度
    private float sweepAngle = 40;//圆弧所占度数

    private float offsweepAngle;

    private int mScrollOffset = 0;

    private RectF mRect;

    /**
     * 直接在代码中调用时，使用该函数
     *
     * @param context
     */
    public CurvedScrollBar(Context context) {
        this(context, null);
    }

    /**
     * 在xml中使用自定义view时，使用这个函数
     *
     * @param context
     * @param attrs
     */
    public CurvedScrollBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    /**
     * 可以由上一个函数中手动调用
     *
     * @param context
     * @param atrrs
     * @param defStyle 自定义函数中的attrs表示view的属性集，defStyle表示默认的属性资源集的id
     */
    public CurvedScrollBar(Context context, AttributeSet atrrs, int defStyle) {
        super(context, atrrs, defStyle);
        initData(context);
    }

    /**
     * 初始化
     *
     * @param context
     */
    private void initData(Context context) {
//        DisplayMetrics dm = getResources().getDisplayMetrics();
//        srcH = dm.widthPixels;

        this.context = context;
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);//设置画笔为无锯齿
        paint.setStrokeWidth(dip2px(context, 7));//线宽
        paint.setStrokeCap(Paint.Cap.ROUND);//开启显示边缘为圆形

        valuePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        valuePaint.setStyle(Paint.Style.STROKE);
        valuePaint.setAntiAlias(true);//设置画笔为无锯齿
        valuePaint.setColor(Color.parseColor("#2EC26C"));
        valuePaint.setStrokeWidth(dip2px(context, 7));//线宽
        valuePaint.setStrokeCap(Paint.Cap.ROUND);//开启显示边缘为圆形
        mRect = new RectF();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        centerX = w / 2;
        centerY = h / 2;
        srcH = w;
        float r = srcH / 2 - valuePaint.getStrokeWidth() * 0.5f;
        mRect.left = -r;//左边
        mRect.top = -r;//上边
        mRect.right = r;//右边
        mRect.bottom = r;//下边
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        float r = srcH / 2 - paint.getStrokeWidth() * 0.5f;
        //移动中心
        canvas.translate(centerX, centerY);
        RectF oval = new RectF();
        oval.left = -r;//左边
        oval.top = -r;//上边
        oval.right = r;//右边
        oval.bottom = r;//下边
        paint.setColor(Color.parseColor("#2E343A"));
        canvas.drawArc(oval, startAngle, sweepAngle, false, paint);    //绘制圆弧

        //画移动小圆弧
        canvas.drawArc(oval, startAngle + offsweepAngle, 20, false, valuePaint);    //绘制圆弧
    }

    /**
     * dip转px
     *
     * @param context
     * @param dpValue
     * @return
     */
    private int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public synchronized void scroll(RecyclerView recyclerView) {
        if(recyclerView==null){
            return;
        }
        int range = 0;
        int temp = recyclerView.computeVerticalScrollRange();//整体的高度，注意是整体，包括在显示区域之外的 会动态变化
        if (temp > range) {
            range = temp;
        }
        int offset = recyclerView.computeVerticalScrollOffset();//已经向下滚动的距离，为0时表示已处于顶部。
        int extent = recyclerView.computeVerticalScrollExtent();//RecycleView显示区域的高度。
        float proportion = (float) (offset * 1.0 / (range - extent));//滑动比例
        float transMaxRange = sweepAngle - 20;//滚动条背景高度-滚动条高度
        offsweepAngle = transMaxRange * proportion;
        postInvalidate();
    }
}
